cc.Class({
    extends: cc.Component,

    properties: {
        cow_prefab: {
            type: cc.Prefab,
            default: null
        },
        audio: {
            type: cc.AudioClip,
            default: null
        }

    },
    startGame(){
        this.checkout.active = false
         // 初始化数据
        this.cows = []
        this.time_num = 60
        this.time_src.string = ''+ this.time_num
        this.count_num = 0
        this.count_src.string = ''+ this.count_num
        // 倒计时
        this.schedule(this.countDown, 1)
        // [3, 6)秒生产一头牛
        this.schedule(this.produceCow, Math.random()*3+3)

    },
    countDown () {
        this.time_num--
        this.time_src.string = ''+ this.time_num
        if (this.time_num <= 0) { // 时间到
          this.unscheduleAllCallbacks() // 停止所有计时器
          // 进入结算
          this.toCheckout()
          return
        }
      },
    produceCow () {
        var cow = cc.instantiate(this.cow_prefab) // 实例化(复制)一头牛
        this.node.addChild(cow)	// 添加到节点中
        this.cows.push(cow) //保存牛
        cow.x = 500	// 初始坐标
        cow.y = 0
    },
    catchCow () {
        var type_index = -1
        for (var i = 0; i < this.cows.length; i++) {
            if (this.cows[i].x > 70 && this.cows[i].x < 150) {  // 套到了牛
              type_index = this.cows[i].getComponent('cow').cow_type
              // 删除该牛
              this.cows[i].removeFromParent()
              this.cows.splice(i, 1)
              // 计数+1
              this.count_num++
              this.count_src.string = '' + this.count_num
              // 音效
              cc.audioEngine.play(this.audio) 
              return type_index
            }
        }
        return -1
    },
    toCheckout () {
        this.checkout.active = true
        this.score.string = '' + this.count_num
        var total = this.count_num
        if (total <= 3) {
            this.title.string = '情场空手'
        } else if (total <= 6) {
            this.title.string = '情场新手'
        } else if (total <= 8) {
            this.title.string = '情场高手'
        } else {
            this.title.string = '情场圣手'
        }
    },
    onLoad () {
        //查找子节点方法：cc.find()或this.node.getChildByName()
        //var tt = this.node.getChildByName('time').getChildByName("seconds").getComponent(cc.Label)
        this.time_src = cc.find('Canvas/time/seconds').getComponent(cc.Label)
        this.count_src = cc.find('Canvas/count/scores').getComponent(cc.Label)
        this.checkout = cc.find('Canvas/checkout')
        this.score = cc.find('Canvas/checkout/mark/score').getComponent(cc.Label)
        this.title = cc.find('Canvas/checkout/mark/title').getComponent(cc.Label)
        this.startGame();
    },

    start () {
        // 设置层级
        this.checkout.zIndex = 3000
        var button = this.node.getChildByName('button')
        //除此之外，还可以调用node.setLocalZOrder()
        button.zIndex = 2000
        var rope = this.node.getChildByName('rope')
        rope.zIndex = 1000
    },

    // update (dt) {},
});
